package com.wen.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.commonutils.GuLiUtils;
import com.wen.servicebase.exception.BusinessException;
import com.wen.ucenter.entity.Member;
import com.wen.ucenter.entity.vo.MemberVO;
import com.wen.ucenter.mapper.MemberMapper;
import com.wen.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wen
 * @since 2021-06-14
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录业务
     *
     * @param member 含手机号和密码
     * @return 成功返回token或失败
     */
    @Override
    public String login(Member member) {
        if (member == null || StringUtils.isEmpty(member.getMobile()) || StringUtils.isEmpty(member.getPassword()))
            throw new BusinessException("用户信息缺失,登陆失败");

//        根据member中的手机号查询是否存在此用户
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", member.getMobile());
        Member member1 = baseMapper.selectOne(wrapper);

//        判断查出来的用户信息是否为null或者密码是否一致
        if (member1 == null || !GuLiUtils.encrypt(member.getPassword()).equals(member1.getPassword()))
            throw new BusinessException("账号名或密码不正确");

//        判断用户是否被禁用或者被删除
        if (member1.getIsDeleted() || member1.getIsDisabled())
            throw new BusinessException("用户不存在,登陆失败");

//        返回token字符串
        return GuLiUtils.getJwtToken(member1.getId(), member1.getNickname());
    }

    /**
     * 注册业务
     *
     * @param memberVO 注册信息
     */
    @Override
    public void register(MemberVO memberVO) {
        if (memberVO == null || StringUtils.isEmpty(memberVO.getMobile())
                || StringUtils.isEmpty(memberVO.getNickname())
                || StringUtils.isEmpty(memberVO.getPassword())
                || StringUtils.isEmpty(memberVO.getCode()))
            throw new BusinessException("用户注册信息缺失,注册失败");

//        判断输入的验证码是否正确
        String code = redisTemplate.opsForValue().get(memberVO.getMobile());
        if (!memberVO.getCode().equals(code)) throw new BusinessException("验证码错误或已过期");
//        判断该用户是否已经注册
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", memberVO.getMobile());
        Member member1 = baseMapper.selectOne(wrapper);
//        如果数据库存在该用户并且没有被禁用和逻辑删除
        if (member1 != null && !member1.getIsDisabled() && !member1.getIsDeleted())
            throw new BusinessException("用户已注册,请前往登录");
//        如果该用户存在数据库并且已经被逻辑删除,当注册时,将逻辑删除字段置为false
        if (member1 != null && !member1.getIsDisabled() && member1.getIsDeleted()) {
            member1.setIsDeleted(false);
            baseMapper.updateById(member1);
//            将redis中的验证码删除
            redisTemplate.delete(memberVO.getMobile());
        }
//        如果该用户在数据库中并不存在,执行新增
        if (member1 == null) {
            Member member = new Member();
            BeanUtils.copyProperties(memberVO, member);
//            密码加密
            member.setPassword(GuLiUtils.encrypt(memberVO.getPassword()));
//            写死头像
            member.setAvatar("https://www.wahaotu.com/uploads/allimg/202009/1600953880160771.jpg");
            baseMapper.insert(member);
//            将redis中的验证码删除
            redisTemplate.delete(memberVO.getMobile());
        }
    }

    /**
     * 根据微信id获取用户信息
     *
     * @param openid 微信id
     * @return 用户对象
     */
    @Override
    public Member getByOpenid(String openid) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 统计某一天的注册人数
     *
     * @param day target day
     * @return result of operation
     */
    @Override
    public int countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
